using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace SimpleServiceBus.Endpoints.ActiveMQ.Transport {

    public class ActiveMQConsumerPoolCollection: IEnumerable<ActiveMQConsumerPool> {

        //-----< Fields >-----
        //----------------------------------------------------------------------------------        
        private Dictionary<String, ActiveMQConsumerPool> fItems = new Dictionary<string, ActiveMQConsumerPool>();
        public const String DefaultConsumerPoolId = "__default__";


        //-----< Constructors >-----
        //----------------------------------------------------------------------------------        
        public ActiveMQConsumerPoolCollection() {
        }

        //-----< Public and protected properties >-----
        //----------------------------------------------------------------------------------        

        public ActiveMQConsumerPool this[String id] {
            get {
                ActiveMQConsumerPool _item;
                fItems.TryGetValue(id, out _item);
                return _item;  
            }
        }

        public Int32 Count {
            get { return fItems.Count; }
        }

        public ActiveMQConsumerPool Default {
            get {
                var _result = this[DefaultConsumerPoolId];
                if(_result == null) {
                    _result = new ActiveMQConsumerPool(DefaultConsumerPoolId);
                    fItems[_result.Id] = _result;
                }
                return _result;
            }
        }

        public Boolean HasConsumers {
            get {
                return this.Any(c => c.HasQueues | c.HasTopics);
            }
        }

        //-----< Public and protected methods >-----
        //----------------------------------------------------------------------------------        

        public void Add(ActiveMQConsumerPool item) {
            fItems.Add(item.Id, item);
        }

        public void Remove(String id) {
            var _item = this[id];
            if(_item != null)
                Remove(_item);
        }

        public void Remove(ActiveMQConsumerPool item) {
            if(item == null)
                throw new ArgumentNullException("item");
            fItems.Remove(item.Id);
        }

        //-----< Events >-----
        //----------------------------------------------------------------------------------        


        //-----< Internal members >-----
        //----------------------------------------------------------------------------------        


        //-----< Private members >-----
        //----------------------------------------------------------------------------------        


        //-----< All nested types >-----
        //----------------------------------------------------------------------------------        


        //-----< All explicit interface implementations >-----
        //----------------------------------------------------------------------------------        



        #region IEnumerable<ActiveMQConsumerPool> Members

        public IEnumerator<ActiveMQConsumerPool> GetEnumerator() {
            return fItems.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        #endregion
    }
}

